package com.cloudsigns.factorymanager2.engineering.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.engineering.adapter.EngineeringConstructCompletionOrderForSuperAdapter;
import com.cloudsigns.factorymanager2.project.activity.AddCompletionOfEvaluationActivity;
import com.cloudsigns.factorymanager2.project.bean.ProductBean;
import com.cloudsigns.factorymanager2.project.customview.GeneralPurposePoupWindow;
import com.cloudsigns.factorymanager2.project.myinerface.ItemClickListener;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;

import java.util.ArrayList;
import java.util.List;

public class EngineeringPublicationCompletionOrderForSuperActivity extends Activity implements View.OnClickListener,ItemClickListener {
    private String TAG=EngineeringPublicationCompletionOrderForSuperActivity.class.getSimpleName();
    private TextView tv_title;
    private ViewGroup rl_back;
    private View view_warranty_periodic;
    private LinearLayout ll_engineering_info_content;
    private ImageView img_warranty_periodic,img_project_select,img_completed_information;
    private TextView edt_warranty_periodic;
    private TextView tv_construction_quality, tv_onstruction_schedule, tv_construction_environment, tv_construction_safe, tv_final_evaluation;
    private String quality, schedule, environment, safe, final_evaluation;
    private LinearLayout ll_evaluation;
    private Button btn_confirm_evaluation;
    private boolean isclickable;
    private TextView tv_add_evaluation;
    private View rl_project;
    private ListView lv;

    private GeneralPurposePoupWindow generalPurposePoupWindow;
    private String datas[] = {"6个月", "12个月","24个月","48个月","60个月"};

    private List<ProductBean> productBeanList = new ArrayList<>();
    private EngineeringConstructCompletionOrderForSuperAdapter adapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
        // 沉浸式状态栏
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                    );
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN

                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);

        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        setContentView(R.layout.layout_engineering_publication_completionorder_for_super);

        init();

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(EngineeringPublicationCompletionOrderForSuperActivity.this, EngineeringCompletionOrderDetailActivity.class);
                startActivity(intent);
            }
        });
    }

    private void init(){
        tv_title = findViewById(R.id.tv_title);
        rl_back = findViewById(R.id.rl_back);
        view_warranty_periodic=findViewById(R.id.view_warranty_periodic);
        ll_engineering_info_content = findViewById(R.id.ll_engineering_info_content);
        img_warranty_periodic=findViewById(R.id.img_warranty_periodic);
        img_project_select = findViewById(R.id.img_project_select);
        img_completed_information=findViewById(R.id.img_completed_information);
        edt_warranty_periodic=findViewById(R.id.edt_warranty_periodic);
        tv_construction_quality = findViewById(R.id.tv_construction_quality);
        tv_onstruction_schedule = findViewById(R.id.tv_onstruction_schedule);
        tv_construction_environment = findViewById(R.id.tv_construction_environment);
        tv_construction_safe = findViewById(R.id.tv_construction_safe);
        tv_final_evaluation = findViewById(R.id.tv_final_evaluation);
        ll_evaluation = findViewById(R.id.ll_evaluation);
        tv_add_evaluation = findViewById(R.id.tv_add_evaluation);
        rl_project = findViewById(R.id.rl_project);
        btn_confirm_evaluation=findViewById(R.id.btn_confirm_evaluation);
        lv=findViewById(R.id.lv);

        tv_title.setText("广告上刊");
        rl_back.setOnClickListener(this);
        img_project_select.setBackgroundResource(R.mipmap.arrow_down_icon);
        img_warranty_periodic.setOnClickListener(this);
        img_completed_information.setBackgroundResource(R.mipmap.arrow_down_icon);
        img_project_select.setOnClickListener(this);
        img_completed_information.setOnClickListener(this);
        tv_add_evaluation.setOnClickListener(this);
        btn_confirm_evaluation.setOnClickListener(this);


        ProductBean bean = new ProductBean();
        bean.setName("工程名称");
        productBeanList.add(bean);

        adapter = new EngineeringConstructCompletionOrderForSuperAdapter(this, TAG);
        adapter.setData(productBeanList);
        lv.setAdapter(adapter);
    }

    private void showEngineeringInfoDialog() {
        if (ll_engineering_info_content.getVisibility() == View.VISIBLE) {
            ll_engineering_info_content.setVisibility(View.GONE);
            img_project_select.setBackgroundResource(R.mipmap.arrow_down_icon);
        } else {
            ll_engineering_info_content.setVisibility(View.VISIBLE);
            img_project_select.setBackgroundResource(R.mipmap.arrow_top);
        }
    }

    private void showEngineeringcompletedInfoDialog() {
        if (lv.getVisibility() == View.VISIBLE) {
            lv.setVisibility(View.GONE);
            img_completed_information.setBackgroundResource(R.mipmap.arrow_down_icon);
        } else {
            lv.setVisibility(View.VISIBLE);
            img_completed_information.setBackgroundResource(R.mipmap.arrow_top);
        }
    }

    private void showWarrantyPeriodicWindow(View anchor) {
        generalPurposePoupWindow = new GeneralPurposePoupWindow(this, datas);
        generalPurposePoupWindow.setItemClick(this);
        generalPurposePoupWindow.showPopupWindow(anchor);
    }

    private void btnClickable(boolean bool) {
        isclickable = bool;
        if (bool) {
            btn_confirm_evaluation.setBackgroundResource(R.mipmap.btn_long_bg_green);
            btn_confirm_evaluation.setTextColor(getResources().getColor(R.color.rgb_ffffffff));
        } else {
            btn_confirm_evaluation.setBackgroundResource(R.mipmap.btn_bg_short_gray);
            btn_confirm_evaluation.setTextColor(getResources().getColor(R.color.rgb_ffffffff));
        }
    }

    @Override
    public void onClick(View view) {
        Intent mIntent;
        switch (view.getId()) {
            // 返回
            case R.id.rl_back:
                finish();
                // 下拉
            case R.id.img_project_select:
                showEngineeringInfoDialog();
                break;
                // 完工信息
            case R.id.img_completed_information:
                showEngineeringcompletedInfoDialog();
                break;
            // 选择质保周期
            case R.id.img_warranty_periodic:
                showWarrantyPeriodicWindow(view_warranty_periodic);
                break;
            // 评价
            case R.id.tv_add_evaluation:
                mIntent = new Intent(EngineeringPublicationCompletionOrderForSuperActivity.this, AddCompletionOfEvaluationActivity.class);
                startActivityForResult(mIntent, ConstantUtil.START_ACTIVITY_KEY_1);
                break;
            // 确认竣工
            case R.id.btn_confirm_evaluation:
                if (isclickable){

                }
                break;

        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case ConstantUtil.START_ACTIVITY_KEY_1:
                if (data != null) {
                    Bundle mBundle = data.getExtras();
                    if (null != mBundle) {
                        quality = mBundle.getString("quality");
                        schedule = mBundle.getString("schedule");
                        environment = mBundle.getString("environment");
                        safe = mBundle.getString("safe");
                        final_evaluation = mBundle.getString("final_evaluation");

                        tv_construction_quality.setText(quality);
                        tv_onstruction_schedule.setText(schedule);
                        tv_construction_environment.setText(environment);
                        tv_construction_safe.setText(safe);
                        tv_final_evaluation.setText(final_evaluation);

                        ll_evaluation.setVisibility(View.VISIBLE);
                    }
                }
                break;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void itemClick(View view, int position) {
        edt_warranty_periodic.setText(datas[position]);
        edt_warranty_periodic.setTextColor(getResources().getColor(R.color.rgb_3333333));
    }
}
